#include "StdAfx.h"
#include "Fraction.h"


Fraction::Fraction(void)
{
}

Fraction::Fraction(int numerator, int denominator)
{
	this->numerator = numerator;
	if(denominator != 0)
		this->denominator = denominator;
}
Fraction::Fraction(Fraction &src)
{
	this->denominator = src.denominator;
	this->numerator = src.numerator;
}

Fraction::~Fraction(void)
{
}

Fraction& Fraction::operator+(Fraction& b)
{
	Fraction &result = *new Fraction();
	int common_denom = this->common_denom(b);

	result.denominator = common_denom;
	int tmp1 = common_denom/this->denominator;
	int tmp2 = common_denom/b.denominator;

	result.numerator = tmp1*this->numerator + tmp2*b.numerator;

	return result;
}
	
Fraction& Fraction::operator-(Fraction& b)
{
	Fraction &result = *new Fraction();
	int common_denom = this->common_denom(b);

	result.denominator = common_denom;
	int tmp1 = common_denom/this->denominator;
	int tmp2 = common_denom/b.denominator;

	result.numerator = tmp1*this->numerator - tmp2*b.numerator;

	return result;
}
	
Fraction& Fraction::operator*(Fraction& b)
{
	Fraction &result = *new Fraction;

	result.numerator = this->numerator * b.numerator;
	result.denominator = this->denominator * b.denominator;

	return result;
}

Fraction& Fraction::operator/(Fraction& b)
{
	Fraction &result = *new Fraction;

	result.numerator = this->numerator * b.denominator;
	result.denominator = this->denominator * b.numerator;

	return result;
}
	
bool Fraction::operator==(Fraction& b)
{
	Fraction &x = this->cancel();
	Fraction &y = b.cancel();

	if(x.denominator == y.denominator && x.numerator == y.numerator)
		return true;
	else return false;
}
	
bool Fraction::operator<=(Fraction& b)
{
	Fraction &x = this->cancel();
	Fraction &y = b.cancel();

	if(x.numerator <= y.numerator)
		return true;
	else return false;
}
	
bool Fraction::operator>=(Fraction& b)
{
	Fraction &x = this->cancel();
	Fraction &y = b.cancel();

	if(x.numerator >= y.numerator)
		return true;
	else return false;
}
	
bool Fraction::operator<(Fraction& b)
{
	Fraction &x = this->cancel();
	Fraction &y = b.cancel();

	if(x.numerator < y.numerator)
		return true;
	else return false;
}
	
bool Fraction::operator>(Fraction& b)
{
	Fraction &x = this->cancel();
	Fraction &y = b.cancel();

	if(x.numerator > y.numerator)
		return true;
	else return false;
}
	
void Fraction::operator=(Fraction& b)
{
	this->denominator = b.denominator;
	this->numerator = b.numerator;
}
	
Fraction::operator int()
{
	return (int)this->numerator/this->denominator;
}
	
Fraction::operator double()
{
	return (double)this->numerator/this->denominator;
}
	
void Fraction::Print()
{
	cout << this->numerator << "/" << this->denominator;
}
	
int Fraction::common_denom(Fraction& b)
{
	int denom1 = this->denominator;
	int denom2 = b.denominator;
	int denom1_tmp = denom1;
	int denom2_tmp = denom2;

	while(denom1_tmp != denom2_tmp)
	{
		if(denom1_tmp < denom2_tmp)
		{
			denom1_tmp += denom1;
		}
		else if(denom1_tmp >= denom2_tmp)
		{
			denom2_tmp += denom2;
		}
		
	}
	return denom1_tmp;
}
	
Fraction& Fraction::cancel()
{
	Fraction &result = *new Fraction;
	//find greates common divisor:
	int gcd = this->gcd();

	result.denominator = this->denominator / gcd;
	result.numerator = this->numerator / gcd;
	return result;
}

int Fraction::gcd()
{
	int a = this->numerator;
	int b = this->denominator;
	if(a == 0)
		return b;
	else{
		while(b != 0)
		{
			if(a > b) a -= b;
			else b -= a;
		}
	}
	return a;
}
